﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;

namespace WPFChatClient
{
    public partial class LoginForm : Window
    {
        private MainWindow mainWindow;
        String username = " ";

        public delegate void LoginDelegate(String message);

        public LoginForm(MainWindow _mainWindow)
        {
            InitializeComponent();
            this.Visibility = Visibility.Visible;
            this.mainWindow = _mainWindow;
        }

        public void showWarning(String warningMsg)
        {
            Dispatcher.Invoke((Action)delegate
            {
                warningLbl.Content = warningMsg;
            });
        }


        private void loginBtn_Click(object sender, EventArgs e)
        {
            username = usernameField.Text;
            if (username.Length < 1)
            {
                showWarning("Username cannot be blank");
                //warningLbl.Text = "Username cannot be blank";
            }
            else
            {
                showWarning("Connecting to server...");
                //warningLbl.Text = "Connecting to server...";
                System.Diagnostics.Debug.WriteLine("connecting");
                mainWindow.loginClient(username);
                System.Diagnostics.Debug.WriteLine("login sent");
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            mainWindow.Close();
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            mainWindow.Close();
            this.Close();

        }

        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
             username = usernameField.Text;
             if (username.Length < 1)
             {
                 showWarning("Username cannot be blank");
                 //warningLbl.Text = "Username cannot be blank";
             }
             else
             {
                 showWarning("Connecting to server...");
                 //warningLbl.Text = "Connecting to server...";
                 System.Diagnostics.Debug.WriteLine("connecting");
                 mainWindow.loginClient(username);
                 System.Diagnostics.Debug.WriteLine("login sent");
             }
        }
    }
}
